// app/service/comment.js

const Service = require('egg').Service;

class CommentService extends Service {
    async addComments() {
        const { ctx } = this
        let { username } = ctx.state.user
        const { slug } = ctx.params
        const { comment } = ctx.request.body;
        const { body } = comment
        console.log('CommentService addComments', body, slug)
        const currentUser = await ctx.model.User.findOne({ username })
        if(currentUser) {
            const currentArticle = await ctx.app.model.Article.findOne({ slug });
            if (!currentArticle) {
                ctx.throw(ctx.errorMap.articleNotFound.code, ctx.errorMap.articleNotFound.message);
            }
            const currentUserId = ctx.app.mongoose.Types.ObjectId(currentUser._id)
            const currentArticleId = ctx.app.mongoose.Types.ObjectId(currentArticle._id)
            const comment = await ctx.app.model.Comment.create({
                body,
                userId: currentUserId,
                articleId: currentArticleId,
            });
            return comment ? comment.toObject() : comment
        } else {
            ctx.throw(ctx.errorMap.notFoundAuthor.code, ctx.errorMap.notFoundAuthor.message);
        }
    }
    async getComments() {
        // 判断是否需要权限
        const { ctx } = this
        const { slug } = ctx.params
        const currentArticle = await ctx.app.model.Article.findOne({ slug });
        if (!currentArticle) ctx.throw(ctx.errorMap.articleNotFound.code, ctx.errorMap.articleNotFound.message);
        const currentArticleId = ctx.app.mongoose.Types.ObjectId(currentArticle._id)
        console.log('CommentService getComments', slug, currentArticleId)
        const comment = await ctx.app.model.Comment.find({
            articleId: currentArticleId
        }).sort({ createdAt: -1 });
        console.log('comment', comment)
        return comment ? comment.map(comment => comment.toJSON()) : comment
    }
    async deleteComment() {
        const { ctx } = this
        let { username } = ctx.state.user
        const { id, slug } = ctx.params
        const currentUser = await ctx.model.User.findOne({ username })
        if(currentUser) {
            const currentArticle = await ctx.app.model.Article.findOne({ slug });
            if (!currentArticle) {
                ctx.throw(ctx.errorMap.articleNotFound.code, ctx.errorMap.articleNotFound.message);
            }
            const currentCommentId = ctx.app.mongoose.Types.ObjectId(id)
            const comment = await ctx.app.model.Comment.findOne({ _id: currentCommentId });
            if(!comment) ctx.throw(ctx.errorMap.notFoundComment.code, ctx.errorMap.notFoundComment.message);
            let result = await ctx.model.Comment.deleteOne({ _id: currentCommentId })
            console.log('result', result)
            return result
        } else {
            ctx.throw(ctx.errorMap.notFoundAuthor.code, ctx.errorMap.notFoundAuthor.message);
        }

    }
}

module.exports = CommentService;